<template>
  <el-dialog
    :title="editType == 0 ? '添加菜单' : '编辑菜单'"
    :visible="show"
    :close-on-press-escape="false"
    :append-to-body="true"
    width="30%"
    top="20%"
    @close="close"
    center>
    <div class="edit-menu-dialog-box">
      <div class="el-input-box">
        <el-input size="medium" v-model="menuName" placeholder="请输入菜单名"/>
      </div>
      <div class="el-input-box" v-if="editContentType == 1">
        <el-input size="medium" v-model="menuUrl" placeholder="请输入链接地址"/>
      </div>
      <div class="el-input-box" v-if="editContentType == 1">
        <el-input size="medium" v-model="menuInfo" placeholder="请输入说明"/>
      </div>
    </div>
    <span slot="footer" class="add-menu-dialog-footer">
      <el-button type="primary" size="medium" @click="commit">{{editType == 0 ? "添 加" : "修 改"}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Api from "@/utils/api.js";

export default {
  /**
   * editType，0：新增，1：编辑
   * show，显示
   * parent，父级菜单
   * item，当前菜单
   * editContentType，0：菜单名，1：菜单名 + url + 说明
   */
  props: ["show", 'parent', 'item', 'editType', 'editContentType'],

  watch: {
    show(newV, oldV) {
      if(newV){
        if(this.editType == 0){
          this.menuName = ''
          this.menuInfo = ''
          this.menuUrl = ''
        }else if(this.editType == 1){
          this.menuName = this.item.menuName
          this.menuInfo = this.item.menuInfo
          this.menuUrl = this.item.menuUrl
        }
      }
    } 
  },

  data(){
    return{
      menuName:'',
      menuInfo:'',
      menuUrl:'',
    }
  },

  methods:{
    close(){
      this.$emit('close')
    },

    commit(){
      // 参数
      let params = this.editType == 0 ? 
      // 新增
      {
        menuName : this.menuName,
        menuInfo : this.menuInfo,
        menuUrl : this.menuUrl,
        parentMenuId : this.parent ? this.parent.id : ''
      } : 
      // 编辑
      {
        id : this.item.id,
        menuName : this.menuName,
        menuInfo : this.menuInfo,
        menuUrl : this.menuUrl,
      }

      // 接口
      let url = this.editType == 0 ? Api.USER_MENU_ADD : Api.USER_MENU_EDIT

      // 请求接口
      this.$axios.post(url, params).then(res =>{
        this.$emit('close')
        this.$emit('addNewMenu', res)
      }).catch(res =>{
        this.$message.warning(res.message);
      })
    },
  }
}
</script>

<style lang="less" scoped>
@import '@/assets/css/index.less';

.edit-menu-dialog-box{
  .el-input-box{
    margin-bottom: 10px;
  }
}

// 添加菜单弹框
.add-menu-dialog-footer{
  
  .el-button{
      width: 200px;
  }
}
</style>
